const Koa = require('koa')
const app = new Koa();
const views = require('koa-views')
const json = require('koa-json')
const onerror = require('koa-onerror')
const bodyparser = require('koa-bodyparser')
const logger = require('koa-logger')
//const wechat = require("../wechat/index");
const wechat = require("./index");
var path = require("path");
var fs = require("fs");
onerror(app)
app.use(logger())
app.use(wechat({ 
    appID: "wx3cac1786c8fb8bb4",
    appSecret: "a4131dadce8711ba251074b1385b78b9",
    token: "ns9u4PAzSoDPkqUzJIo5xQ0L",
    mode:'clear',
    encodingAESKey:""
}, async function() {
    var wechat = this.wechat;
    var token = await wechat.getAccessToken();
    var message = this.message.Content;
    if (message == "1") {
        this.reply = {
            type: "text",
            content: token
        }
    } else if (message == "2") {
        wechat.expiresIn = Date.now() - 100000;
        this.reply = {
            type: "text",
            content: "更新成功"
        }
    }
    //创建菜单
    else if (message == "3") {
        var result = await wechat.createMenu(menuData);
        console.log(result);
    } else if (message == "4") {
        var result = await wechat.getMenu();
        this.reply = {
            type: "text",
            content: JSON.stringify(result)
        }
    } else if (message == "5") {
        var result = await wechat.deleteMenu();
        this.reply = {
            type: "text",
            content: JSON.stringify(result)
        }
    } else if (message == "6") {
        var result = await wechat.uploadTemporaryMaterial("image", fs.createReadStream("./test.jpeg"));
        this.reply = {
            type: "text",
            content: JSON.stringify(result)
        }
    }
}))

app.listen(3000, () => {
    console.log("server is running...");
})